$(function () {
    $.showLoading("请稍等片刻...");

    window.app = {};
    window.app.util = {
        getUrlParam: function (key) {
            var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        },
        isBusy: false,
        restClient: function (url, data, successFun) {
            if (window.app.util.isBusy) return;
            window.app.util.isBusy = true;
            $.ajax({
                type: 'POST',
                contentType: 'application/json;charset=UTF-8',
                url: url,
                data: data || '{}',
                beforeSend: function (xhr) {
                    var token = '';
                    if (token) {
                        xhr.setRequestHeader('X-AUTH-TOKEN', token);
                    }
                },
                complete: function () {
                    window.app.util.isBusy = false;
                },
                success: function (data) {
                    if (data && data.status == 0) {
                        if (successFun) {
                            successFun(data);
                        }
                    }
                }
            });
        }
    };
    window.app.route = {
        channel: {
            htmlUrl: '/mobile/html/website/channel.html'
        },
        docList: {
            htmlUrl: '/mobile/html/website/docList.html'
        },
        docDetail: {
            htmlUrl: '/mobile/html/website/docDetail.html'
        }
    };
    window.app.page = {
        currentRoute: window.app.route[window.app.util.getUrlParam('page')],
        index: {
            divAppContainer: $('#appContainer')
        }
    };
    window.app.fn = {
        loadHtml: function (container, htmlUrl) {
            container.load(htmlUrl, function (response, status, xhr) {
                if (status == 'success') {
                    window.app.page.currentRoute.successFun();
                } else {
                    console.log(xhr);
                }
            });
        }
    };

    window.app.fn.loadHtml(window.app.page.index.divAppContainer,
        window.app.page.currentRoute.htmlUrl + '?time=' + (+new Date()));
});